/**
 * longtermstate.js contains java script functions which determine the dynamic
 * behavior of the Long-Term State Page
 *  
 * @author Eric Hoffman
 * @version 4/14/08
 */
 
 
/**
 * Toggles the radio buttons and colors of the input fields to indicate which of
 * the two date input methods is active.
 */
function dateEnableToggle(disorenable) {
	if (disorenable == "date") {
		document.getElementById("dateNUnits").style.backgroundColor = "white";
		document.getElementById("dateSelect").style.backgroundColor = "white";
		document.getElementById("calStartDateBox").style.backgroundColor="#CCCCCC";
		document.getElementById("calEndDateBox").style.backgroundColor="#CCCCCC";
		document.getElementById("r1").checked = true;
	} 
	else { //=='cal'
		document.getElementById("dateNUnits").style.backgroundColor = "#CCCCCC";
		document.getElementById("dateSelect").style.backgroundColor = "#CCCCCC";
		document.getElementById("calStartDateBox").style.backgroundColor="white";
		document.getElementById("calEndDateBox").style.backgroundColor="white";
		document.getElementById("r2").checked = true;
	}
}

/**
 * Prevents IE from displaying a history of previously entered dates over top of 
 * the calendar when the calendar date boxes are clicked directly.
 */
function calBlur() {
	document.getElementById("r2").checked = true;
	document.getElementById("dateNUnits").style.backgroundColor = "#CCCCCC";
	document.getElementById("dateSelect").style.backgroundColor = "#CCCCCC";
	document.getElementById("calStartDateBox").style.backgroundColor = "white";
	document.getElementById("calEndDateBox").style.backgroundColor = "white";
	document.getElementById("calStartDateBox").blur();
	document.getElementById("calEndDateBox").blur();
}

/**
 * When a new date is selected from the calendar this function makes sure the 
 * start date is before the end date.  If it is not it adjust the other date to 
 * be one day after or before the newly entered date, depending on which date is
 * being changed.
 */
function catcalc(cal) {
	var calDate = cal.date;
	var time = calDate.getTime();
	var activeField = cal.params.inputField;
	if (activeField == document.getElementById("calEndDateBox")) {//if changed end date
		var calEndDate = new Date(time);
		var tmp = Date.parse(document.getElementById("calStartDateBox").value);
		var calStartDate = new Date(tmp);
	} 
	else { //if changed start date
		var calStartDate = new Date(time);
		var tmp = Date.parse(document.getElementById("calEndDateBox").value);
		var calEndDate = new Date(tmp);
	}
	if (calStartDate > calEndDate) { //fix dates if start is after end
		if (activeField == document.getElementById("calEndDateBox")) {//if changed end date
			calStartDate.setTime(calEndDate.getTime() - 86400000); //bump by one day
			var month = calStartDate.getMonth() + 1;
			if (month < 10) {
				month = "0" + month;
			}
			var day = calStartDate.getDate();
			if (day < 10) {
				day = "0" + day;
			}
			var hours = calStartDate.getHours();
			if (hours < 10) {
				hours = "0" + hours;
			}
			var minutes = calStartDate.getMinutes();
			if (minutes < 10) {
				minutes = "0" + minutes;
			}
			var dateStr = calStartDate.getFullYear() + "/" + month + "/" + day + 
			  " " + hours + ":" + minutes;
			document.getElementById("calStartDateBox").value = dateStr;
			
		}
		else { //if changed start date
			calEndDate.setTime(calStartDate.getTime() + 86400000); //bump by one day
			var month = calEndDate.getMonth() + 1;
			if (month < 10) {
				month = "0" + month;
			}
			var day = calEndDate.getDate();
			if (day < 10) {
				day = "0" + day;
			}
			var hours = calEndDate.getHours();
			if (hours < 10) {
				hours = "0" + hours;
			}
			var minutes = calEndDate.getMinutes();
			if (minutes < 10) {
				minutes = "0" + minutes;
			}
			var dateStr = calEndDate.getFullYear() + "/" + month + "/" + day + 
			  " " + hours + ":" + minutes;
			document.getElementById("calEndDateBox").value = dateStr;
		}
	}
}

/**
 * Expands and cloapses nodes in the sensor tree.
 */
function initMenus() {
	if (!document.getElementsByTagName) {
		return;
	}
	var aMenus = document.getElementsByTagName("LI");
	for (var i = 0; i < aMenus.length; i++) {
		var mclass = aMenus[i].className;
		if (mclass.indexOf("treenode") > -1) {
			var submenu = aMenus[i].childNodes;
			for (var j = 0; j < submenu.length; j++) {
				if (submenu[j].tagName == "A") {
					submenu[j].onclick = function () {
						var node = this.nextSibling;
						while (1) {
							if (node != null) {
								if (node.tagName == "UL") {
									var d = (node.style.display == "none");
									node.style.display=(d)?"block":"none";
									this.className=(d)?"treeopen":"treeclosed";
									return false;
								}
								node = node.nextSibling;
							} 
							else {
								return false;
							}
						}
						return false;
					};
					submenu[j].className = (mclass.indexOf("open") > -1)?
					  "treeopen":"treeclosed";
				}
				if (submenu[j].tagName == "UL") {
					submenu[j].style.display = (mclass.indexOf("open") > -1)?
					  "block":"none";
				}
			}
		}
	}
}

window.onload = initMenus;
